package xc.com.cms.client.service.impl;

import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;
import xc.com.cms.client.repository.CmsPageRepository;
import xc.com.cms.client.repository.CmssiteRepository;
import xc.com.cms.client.service.CmsPageService;
import xc.com.cms.domain.CmsPage;
import xc.com.cms.domain.CmsSite;
import xc.com.cms.response.CmsCode;
import xc.com.exception.ExceptionCast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

@Service
public class CmsPageServiceImpl implements CmsPageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CmsPageService.class);

    @Autowired
    private CmsPageRepository cmsPageRepository;
    @Autowired
    private GridFsTemplate gridFsTemplate;
    @Autowired
    private GridFSBucket gridFSBucket;
    @Autowired
    private CmssiteRepository cmssiteRepository;
    @Override
    public void savepagetoservicepath(String CmsPageId) {
        //页面信息
        CmsPage cmsPage = this.querycmspagebyid(CmsPageId);
        //页面所属站点信息
        CmsSite cmsSite = this.querycmssitebyid(cmsPage.getSiteId());
        //页面物理路径
        String path=cmsSite.getSitePhysicalPath()+cmsPage.getPagePhysicalPath()+cmsPage.getPageName();
        //静态化文件
        String htmlFileId = cmsPage.getHtmlFileId();
        InputStream inputStream = this.getfilebyid(htmlFileId);
        //判断是否为空
        if(inputStream == null){
            ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_HTMLISNULL);
        }
        //创建文件输出流
        FileOutputStream fileOutputStream=null;
        try {
           fileOutputStream  = new FileOutputStream(new File(path));
            IOUtils.copy(inputStream, fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
                fileOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public CmsPage querycmspagebyid(String CmsPageId) {
        Optional<CmsPage> s = cmsPageRepository.findById(CmsPageId);
        if(s.isPresent()){
            return s.get();
        }
        return null;
    }
    @Override
    public CmsSite querycmssitebyid(String CmsStieId) {
        Optional<CmsSite> s = cmssiteRepository.findById(CmsStieId);
        if(s.isPresent()){
            return s.get();
        }
        return null;
    }
    @Override
    public InputStream getfilebyid(String FieleId)  {
        //文件对象
        GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(FieleId)));
        //打开文件下载流
        GridFSDownloadStream gridFSDownloadStream=gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
        GridFsResource gridFsResource=new GridFsResource(gridFSFile,gridFSDownloadStream);
        try {
            return gridFsResource.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
